package structTool

import (
	"Gin-Server-01/com/server/core/logs"
	"fmt"
	"reflect"
)

/**
 * @Author Administrator
 * @Description
 * @Date 2023/12/8 8:45
 * @Version 1.0
 */

// IsStructEmpty
//
//	@Description: 判断结构体的字段是否为空
//	@param: obj 结构体
//	@return isEmpty 为空为true
//	@return msg 返回的消息
func IsStructEmpty(obj interface{}) (isEmpty bool, msg string) {
	if obj == nil {
		return true, "结构体为空"
	}
	typeOf := reflect.TypeOf(obj)
	valueOf := reflect.ValueOf(obj)

	for i := 0; i < typeOf.NumField(); i++ {
		field := typeOf.Field(i)
		value := valueOf.Field(i)
		logs.Println(fmt.Sprintf("字段名称: %s, 字段类型: %s 字段数据%s:\n", field.Name, field.Type, value))
		if len(value.String()) == 0 {
			return false, fmt.Sprintf("字段%s为空", field.Name)
		}
	}

	return true, ""
}
